// ==UserScript==
// @name         氢水电
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.0.4
// @description  Let's use better Hydro
// @author       lemon_qwq
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// ==/UserScript==
/* ==UserConfig==
setting:
  background:
    title: 背景图片
    description: 请填写背景图片URL，不使用则不填
    default: https://cdn.luogu.com.cn/images/bg/fe/Day_And_Night_1.jpg
  background-size:
    title: 背景图片填充方式
    description: 请选择背景图片填充方式
    type: select
    default: 填充
    values: [自动,填充,适应]
  background-color:
    title: 背景颜色
    description: 请填写当背景图片无法加载时的背景颜色
    default: #edf0f2
  opacity:
    title: 卡片透明度
    description: 请填写卡片透明度
    type: number
    default: 0.8
    min: 0
    max: 1
  opacity2:
    title: 鼠标悬停时的透明度
    description: 请填写鼠标悬停时的透明度
    type: number
    default: 1
    min: 0
    max: 1
  border:
    title: 圆角半径
    description: 请填写圆角半径
    type: number
    default: 8
    min: 0
    max: 20
    unit: px
  menu:
    title: 菜单栏背景颜色
    description: 请填写菜单栏背景颜色
    default: ffffffaa
  menu-blur:
    title: 菜单栏背景模糊程度
    description: 请填写菜单栏背景模糊程度，不使用则为0
    type: number
    default: 5
    min: 0
    max: 100
orther:
  music:
    description: 是否启用歌曲组件
    type: checkbox
    default: false
  musicURL:
    title: 歌曲链接
    description: 请填写歌曲链接
    default: https://music.163.com/outchain/player?type=2&id=5221167&auto=0&height=66
  welcome:
    description: 是否启用欢迎栏
    type: checkbox
    default: true
  logo:
    title: 自定义图标
    description: 请填写自定义图标，不用则不填
    default: 
  fish:
    description: 是否启用敲木鱼
    type: checkbox
    default: true
  fish-text:
    title: 敲木鱼文子
    description: 请填写敲木鱼文子
    default: rp++
    max: 7
 ==/UserConfig== */
(function() {
    'use strict';
    var bg=GM_getValue("setting.background","https://cdn.luogu.com.cn/images/bg/fe/Day_And_Night_1.jpg");
    var bgs=GM_getValue("setting.background-size","填充");if(bgs=='自动')bgs="auto";if(bgs=="填充")bgs="cover";if(bgs=="适应")bgs="contain";
    var bgc=GM_getValue("setting.background-color","#edf0f2");
    var menu=GM_getValue("setting.menu","ffffffaa");
    var mb=GM_getValue("setting.menu-blur","ffffffaa");
    var opacity=GM_getValue("setting.opacity","0.8");
    var opacity2=GM_getValue("setting.opacity2","1");
    var border=GM_getValue("setting.border","8");
	var username=Array.from(document.querySelectorAll('li[data-dropdown-target="#menu-nav-user"]')).map(item=>item.querySelector('a')).find(link=>link&&link.textContent.trim()!='Language')?.textContent.trim()||'';
    const name=document.title.split("-").pop().trim();
    if(document.querySelector("html").dataset.app=='Hydro'){
        var css="";
        css+=[
            `#panel{`,
            `   background-image:url(${bg})!important;`,
            `   background-attachment:fixed!important;`,
            `   background-size:${bgs}!important;`,
            `   background-position:center!important;`,
            `   background-color:${bgc}!important`,
            `}`,
            `.section{`,
            `   opacity:${opacity}!important;`,
            `   border-radius:${border}px!important;`,
            `   overflow:hidden!important;`,
            `}`,
            `.section:hover{`,
            `   opacity:${opacity2}!important;`,
            `   transform:translateY(-5px)!important;`,
            `}`,
            `#menu{`,
            `   background:#${menu}!important;`,
            `   backdrop-filter:blur(${mb}px)!important;`,
            `}`,
            `#menu:hover{`,
            `   background:#fff!important;`,
            `}`,
            `@keyframes floatUp {`,
            `    0% { opacity: 1; transform: translateY(0); }`,
            `    100% { opacity: 0; transform: translateY(-100px); }`,
            `}`,
            `.floating-text {`,
            `    position: absolute;`,
            `    font-size: 20px;`,
            `    font-family: "Comic Sans MS"!important;`,
            `    color: #ff6347;`,
            `    animation: floatUp 1s ease-out forwards;`,
            `    pointer-events: none;`,
            `}`,
            `@keyframes shake {`,
            `    0% { transform: rotate(0deg); }`,
            `    25% { transform: rotate(8deg); }`,
            `    50% { transform: rotate(0deg); }`,
            `    75% { transform: rotate(-8deg); }`,
            `    100% { transform: rotate(0deg); }`,
            `}`,
            `.shake {`,
            `    animation: shake 0.3s ease-in-out;`,
            `}`,
        ].join('\n');
        setTimeout((function(){
            if(typeof GM_addStyle!="undefined") {
                GM_addStyle(css);
            }else if(typeof PRO_addStyle!="undefined") {
                PRO_addStyle(css);
            }else if(typeof addStyle!="undefined") {
                addStyle(css);
            }else {
                var node=document.createElement("style");
                node.type="text/css";
                node.appendChild(document.createTextNode(css));
                var heads=document.getElementsByTagName("head");
                if(heads.length>0) {
                    heads[0].appendChild(node);
                }else{
                    document.documentElement.appendChild(node);
                }
            }
        }),500)
		if(GM_getValue("orther.music",false)){
            const musicURL=GM_getValue("orther.musicURL","");
            const newHTML=`<div class="section side visible"><div class="section__body typo"><iframe frameborder="no"border="0"marginwidth="0"marginheight="0"width="100%"height="80"src="${musicURL}"></iframe></div></div>`;
            const row=document.getElementById('panel').querySelector('.main').querySelector('.row');
            const divs=row.querySelectorAll(':scope > div');
            divs.forEach(div=>{
                const className=div.className;
                if(className.includes('medium-3')&&className.includes('columns')){
                    div.insertAdjacentHTML('afterbegin',newHTML)
                }
            });
        }
		if(GM_getValue("orther.welcome",true)){
            console.log('shan');
            const newHTML=`<div class="section visible"><div class="section__header"><h1 class="section__title">欢迎 ${username} 来到 ${name}!</h1></div></div>`;
            const main=document.querySelector('#panel').querySelector('.main');
            const element=main.querySelector('div');
            element.insertAdjacentHTML('afterbegin',newHTML);
        }
        var ico=GM_getValue("orther.logo","");
        if(ico!=""){
            const element=document.querySelector("#menu").querySelector(".nav__logo");
            element.src=ico;
        }
        if(GM_getValue("orther.fish",true)){
            const newHTML=`<div class="fish section side visible" style="height:200px;padding-top:100px;padding-left:70px;background:#000000ff;color:#fff"><div class="section__header"><img src="https://img.z4a.net/images/2025/01/04/b7c572e046da8f9b73e4c43a3f232d7f.png" style="width:100px;user-select: none;"></img></div><h style="position: absolute; right: 10px; bottom: 10px; margin: 5px;">今日已敲0次</h></div>`;
            const row=document.getElementById('panel').querySelector('.main').querySelector('.row');
            const divs=row.querySelectorAll(':scope > div');
            divs.forEach(div=>{
                const className=div.className;
                if(className.includes('medium-3')&&className.includes('columns')){
                    div.insertAdjacentHTML('afterbegin',newHTML)
                }
            });
            const element=document.querySelector('.fish');  
            const element2=element.querySelector('img');
            element2.addEventListener('click', function(event) {
                element2.classList.add('shake');
                setTimeout(()=>{
                    element2.classList.remove('shake');
                }, 300);
                const floatText=document.createElement('div');
                floatText.classList.add('floating-text');
                floatText.textContent=GM_getValue("orther.fish-text","rp++");
                floatText.style.left=`${element2.offsetLeft+element2.offsetWidth/2+20+Math.random()*30}px`;
                floatText.style.top=`${element2.offsetTop+70}px`;
                element.appendChild(floatText);
                setTimeout(() => {
                    element.removeChild(floatText);
                }, 1000); 
                GM_setValue("fish-count",GM_getValue("fish-count",0)+1);
            });
            setInterval((function(){
                var element3=element.querySelector('h');
                element3.textContent = `今日已敲${GM_getValue("fish-count",0)}次`;
            }),1);
            (function(){
                var time=(new Date())-(new Date(0));
                time=Math.floor(time/(24*60*60*1000));
                console.log(time);
                if(time!=GM_getValue("fish-lasttime",0)){
                    GM_setValue("fish-lasttime",time);
                    GM_setValue("fish-count",0);
                }
            })();
        }
    }
})();